Skip to main content

df[]

df[condition]

Creates a new DataFrame containing only the rows that satisfy specific conditions.

Input:
condition : bool
A sequence of boolean values with length equal to the number of rows of the DataFrame.
Returns:
new_df - A new DataFrame that contains only the rows whose corresponding element in condition is True.
Return Type:
DataFrame

pets
IndexIDSpeciesColorWeightAgeIs_CatOwner_Comment
0dog_001dogblack405False There are no bad dogs, only bad owners.
1cat_001catgolden1.50.2TrueMy best birthday present ever!!!
2cat_002catblack159True****All you need is love and a cat.****
3dog_002dogwhite802FalseLove is a wet nose and a wagging tail.
4dog_003dogblack250.5FalseBe the person your dog thinks you are.
5ham_001hamsterblack13FalseNo, thank you!
6ham_002hamstergolden0.250.2FalseNo, thank you!
7cat_003catblack100TrueNo, thank you!

Series of bools of if Species is a dog AND Color is white

(pets.get('Species')=='dog') & (pets.get('Color')=='white')
  • 0False
  • 1False
  • 2False
  • 3True
  • 4False
  • 5False
  • 6False
  • 7False

DataFrame where Species is a dog AND Color is white

pets[(pets.get('Species')=='dog') & (pets.get('Color')=='white')]
IndexIDSpeciesColorWeightAgeIs_CatOwner_Comment
3dog_002dogwhite802FalseLove is a wet nose and a wagging tail.

Series of bools where Species is a dog OR Color is white

(pets.get('Species')=='dog') | (pets.get('Color')=='white')
  • 0True
  • 1False
  • 2False
  • 3True
  • 4True
  • 5False
  • 6False
  • 7False

DataFrame where Species is a dog OR Color is white

pets[(pets.get('Species')=='dog') | (pets.get('Color')=='white')]
IndexIDSpeciesColorWeightAgeIs_CatOwner_Comment
0dog_001dogblack405False There are no bad dogs, only bad owners.
3dog_002dogwhite802FalseLove is a wet nose and a wagging tail.
4dog_003dogblack250.5FalseBe the person your dog thinks you are.

Some other examples!

pets[pets.get('Weight') >= 25]
IndexIDSpeciesColorWeightAgeIs_CatOwner_Comment
0dog_001dogblack405False There are no bad dogs, only bad owners.
3dog_002dogwhite802FalseLove is a wet nose and a wagging tail.
4dog_003dogblack250.5FalseBe the person your dog thinks you are.
pets[pets[(pets.get('Weight') >= 25) &  (pets.get('Weight') < 80)]]
IndexIDSpeciesColorWeightAgeIs_CatOwner_Comment
0dog_001dogblack405False There are no bad dogs, only bad owners.
4dog_003dogblack250.5FalseBe the person your dog thinks you are.
pets[pets[pets.get('Color').str.contains('e')]]
IndexIDSpeciesColorWeightAgeIs_CatOwner_Comment
1cat_001catgolden1.50.2TrueMy best birthday present ever!!!
3dog_002dogwhite802FalseLove is a wet nose and a wagging tail.
6ham_002hamstergolden0.250.2FalseNo, thank you!
pets[pets1 = pets.set_index('ID')
pets1[pets1.index.str.contains('cat')]]
IndexSpeciesColorWeightAgeIs_CatOwner_Comment
cat_001catgolden1.50.2TrueMy best birthday present ever!!!
cat_002catblack159True****All you need is love and a cat.****
cat_003catblack100TrueNo, thank you!
pets[pets.index > 3]
IndexIDSpeciesColorWeightAgeIs_CatOwner_Comment
4dog_003dogblack250.5FalseBe the person your dog thinks you are.
5ham_001hamsterblack13FalseNo, thank you!
6ham_002hamstergolden0.250.2FalseNo, thank you!
7cat_003catblack100TrueNo, thank you!